import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class HoldingTime {
    private String startingTime;
    private String endingTime;
    private long minute;//通话时长
    public Date startT;
    public Date endT;
    private String springtime;
    private String summertime;
    public  HoldingTime(String startingTime,String endingTime,String st,String sa){
        this.startingTime = startingTime;
        this.endingTime = endingTime;
        this.startT = StrToDate(this.startingTime);
        this.endT = StrToDate(this.endingTime);
        this.springtime=st;
        this.summertime=sa;
    }

    public Date StrToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    public String DateFormate(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
        String sdate = format.format(date);
        return sdate;
    }
    public long getHoldingTime(){
        long between;
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(startT);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(endT);

        between = cal2.getTime().getTime()-cal1.getTime().getTime();
        if(between<0) {
            if(summertime.equals("0")){
                return 0;
            }
            minute = ((cal2.getTime().getTime() - cal1.getTime().getTime()) / 1000) / 60;//得到通话时长
        }
        else
            minute = (between/1000+59)/60;
//        if(minute>1800 || minute <0)
//            return 0;
        //判断接通时间是否在发生转换的日期
        if(springtime.equals("1")&&summertime.equals("1")){
            return 0;
        }
        if(springtime.equals("1")){
            minute -=60;
        }
        //将位于转换的时间段内的时间都看作已经转换之后的
        if(summertime.equals("1")){
            minute +=60;
        }
        if(minute>1800 || minute <0){
//            System.out.println("时间错误，时限范围不对！");
            return 0;
        }else{
            return minute;
        }
    }
}